package com.atguigu.gmall.cart.rp;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.execption.GmallException;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.utils.RequestUtils;
import com.atguigu.gmall.product.entity.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName CartRpcController
 * @Description TODO
 * @Author king
 * @Date 2022/11/16 14:02
 * @Version 1.0
 */
@RequestMapping("/api/inner/rpc/cart")
@RestController
public class CartRpcController {

    @Autowired
    CartService cartService;

    /**
     * 把商品添加到微服务
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/add/{skuId}")
    public Result<SkuInfo> addToCart(@PathVariable("skuId") Long skuId,
                                     @RequestParam("skuNum") Integer skuNum,
                                     HttpServletRequest request){

        //1,怎么知道是哪个人要加入购物车
        Long uid = RequestUtils.uid();
        String tempid = RequestUtils.tempid();
        //2,获取这个人购物车key
        String cartKey = cartService.determinCartKey();

        //3,把一个商品添加到购物车
        SkuInfo skuInfo = null;
        skuInfo = cartService.addItemToCart(skuId,skuNum,cartKey);


        return Result.ok(skuInfo);
    }

    @GetMapping("/deleteChecked")
    public Result deleteChecked(){
        String cartKey = cartService.determinCartKey();
        cartService.deleteChecked(cartKey);

        return Result.ok();
    }

}
